<template>
	<view class="page">
		<view class="top-bg"></view>
		<view class="list-box">
			<view class="item-box choose-site" @tap="onAddressList">
				<view class="address-box" v-if="consignee.length > 0">
					<view class="address-box_top">
						<text class="item-label" v-if="is_default">默认</text>
						<text class="item-name">{{consignee}}</text>
						<text class="item-phone">{{mobile}}</text>
					</view>
					<view class="address-box_bottom">{{address}}</view>
				</view>
				<view class="item-box_label" v-else>选择收货地址</view>
				<uni-icons type="right" size="32rpx" color="#999999"></uni-icons>
			</view>
			<view class="item-box" @tap="goDelivery">
				<text class="item-box_label">配送方式</text>
				<view class="l-f">
					<text class="item-box_value">{{delivery && delivery.delivery_name ? delivery.delivery_name : '请选择配送方式'}}</text>
					<uni-icons type="right" size="32rpx" color="#999999"></uni-icons>
				</view>
			</view>
			<view class="item-box">
				<text class="item-box_label">发票</text>
				<view class="l-f" @tap="onTax">
					<text class="item-box_value">{{tax && tax.name ? tax.name : '不开票'}}</text>
					<uni-icons type="right" size="32rpx" color="#999999"></uni-icons>
				</view>
			</view>
			<view class="item-box">
				<text class="item-box_label">商品合计</text>
				<text class="item-box_value">￥{{totalObj.goods_price}}</text>
			</view>
			<view class="item-box">
				<text class="item-box_label">包装费</text>
				<text class="item-box_value">￥{{totalObj.integral}}</text>
			</view>
			<view class="item-box">
				<text class="item-box_label">运费</text>
				<text class="item-box_value">￥{{totalObj.shipping_fee}}</text>
			</view>
			<view class="item-box">
				<text class="item-box_label">截单时间</text>
				<switch style="transform: scale(.9);" :checked="isChecked" @change="onChange" />
			</view>
			<view class="item-box">
				<text class="item-box_label">活动优惠</text>
				<text class="item-box_value">￥{{discountObj.discount}}</text>
			</view>
			<!-- 商品 -->
			<view class="goods-detail">
				<view class="l-f goods-item"
				v-for="(item,index) in goodsList"
				:key="index">
					<image class="goods-item_cover" :src="item.goods_thumb"></image>
					<view class="goods-item_info">
						<view class="goods-name u-line-1">{{item.goods_name}}</view>
						<view class="goods-sku">{{item.sku_str}}</view>
						<view class="goods-price">{{item.goods_price}}</view>
					</view>
					<view class="goods-item_number">X{{item.goods_number}}</view>
				</view>
			</view>
			<!-- 留言 -->
			<view class="leave-box">
				<view class="leave-box_title">给卖家留言</view>
				<u--textarea :maxlength="100" v-model="leave" placeholder="请输入留言内容" ></u--textarea>
			</view>
		</view>
		<!-- 确认订单 -->
		<view class="bottom-box">
			<view class="bottom-box_text">
				实付: ￥{{totalMoney}}
			</view>
			<view class="btn-box" @tap="onSubmit">
				确认订单
			</view>
		</view>
	</view>
</template>

<script>
	import {getOrderDetailApi,getOrderEndApi,getOrderDiscountApi,postOrderSaveApi} from '@/api/order.js'
	import {mapState} from 'vuex'
	export default {
		computed: {
			...mapState(['user_id','token']),
			totalMoney: function() {
				let money = this.totalObj.total_fee - this.discountObj.discount
				return money.toFixed(2)
			}
		},
		data() {
			return {
				goods_id: null,
				number: 0,
				address_list: {}, // 地址
				goodsList: [], // 商品列表
				totalObj: {}, // 总统计
				discountObj: {}, // 折扣优惠
				isChecked: false,
				leave:'',
				is_default: false,
				consignee: '',
				mobile: '',
				address: '',
				delivery: {}, // 配送方式
				tax: [],
			};
		},
		onLoad({goods_id = '', number = '',consignee = '',mobile = '',address = '',is_default = false}) {
			this.goods_id = goods_id
			this.number = number
			this.consignee = consignee
			this.mobile = mobile
			this.address = address
			this.is_default = is_default === 'true' ? true : false
		},
		onShow() {
			this.getOrderDetail()
			this.getOrderEnd()
			this.getOrderDiscount()
		},
		methods: {
			async onSubmit() {
				if(!this.consignee) {
					uni.showToast({
						title: '请选择收货地址',
						icon: 'none'
					})
					return
				}
				if(JSON.stringify(this.delivery) === '{}') {
					uni.showToast({
						title: '请选择配送方式',
						icon: 'none'
					})
					return
				}
				// 判断
				if(!this.isChecked) {
					uni.showToast({
						title: '请同意明天配送',
						icon: 'none'
					})
					return
				}
				let inv_payee = this.tax.rise ? this.tax.rise : '',
					tax_num = this.tax.number ? this.tax.number : ''
				const res = await postOrderSaveApi({
					user_id: this.user_id,
					token: this.token,
					address_id: this.address_list.address_id,
					shipping_id: this.delivery.delivery_id,
					delivery_name: this.delivery.delivery_name,
					delivery_id: this.delivery.delivery_id,
					inv_payee,
					tax_num,
					msg: this.leave, // 留言
					order_amount: this.totalMoney,
					
					productId: '',
					bonus_id: '',
					point: 0,
					p_type: 1,
					o_time: '',
					inv_payee: '',
					tax: '',
					tax_type: 'n',
					ral: false,
					superpack: false,
					superGoodsid: '',
					integralGoodsid: '',
					pack_fee: 0,
					pack_name: '',
					inv_type: '',
					result_spike: '',
					goods_id: '',
					pintuan: '',
					pintuanGoodsId: '',
					pintuanNumber: '',
					pintuanOrderId: '',
					sku: '',
					jifennum: '',
					
					// #ifdef MP-WEIXIN
					platform: 'MP-WEIXIN',
					// #endif
					// #ifdef H5
					platform: 'H5',
					// #endif
					// #ifdef APP-PLUS
					platform: 'APP-PLUS',
					// #endif
				})
				uni.navigateTo({
					url: `/pages/aPackage/pay/pay?order_id=${res.data.info.order_id}&price=${this.totalMoney}`
				})
			},
			goDelivery() {
				uni.$off('chooseDelivery')
				uni.$on('chooseDelivery',e => {
					console.log(e)
					this.delivery = e
				})
				let delivery_id = ''
				if(this.delivery) {
					delivery_id = this.delivery.delivery_id
				}
				uni.navigateTo({
					url: `/pages/aPackage/delivery/delivery?address_id=${this.address_list.address_id}&delivery_id=${delivery_id}`
				})
			},
			onTax() {
				uni.$off('chooseTax')
				uni.$on('chooseTax',e => {
					this.tax = e
				})
				uni.navigateTo({
					url: '/pages/aPackage/tax/tax'
				})
			},
			onAddressList() {
				uni.navigateTo({
					url: '/pages/aPackage/addressList/addressList'
				})
			},
			onChange() {
				this.isChecked = !this.isChecked
			},
			// 获取订单详情
			async getOrderDetail() {
				const res = await getOrderDetailApi({
					user_id: this.user_id,
					token: this.token,
					address_id: '',
					shipping_id: '',
					tax_type: '', // 发票类型
					ral: '', //是否是积分商品
					integralGoodsid: '', // 积分商品id
					super: '', // 是否是
					superGoodsid: '', // 超值礼包id
					p_type: '', // 配送类型
					goods_id: this.goods_id, // 商品id
					result_spike: '', // 是否秒杀商品
					pintuan: '', // 是否为拼团商品
					pintuanGoodsId: '', // 拼团商品id
					number: this.number, // 数量
				})
				const {address_list = {}, goods_list = [], total = {}} = res.data.info
				this.address_list = address_list
				this.goodsList = goods_list
				this.totalObj = total
				
				this.is_default = address_list.is_default === 'true' ? true : false
				this.consignee = address_list.consignee
				this.mobile = address_list.mobile
				this.address = address_list.address
			},
			// 获取订单截止时间
			async getOrderEnd() {
				const res = await getOrderEndApi({
					user_id: this.user_id,
					token: this.token
				})
				console.log(res)
			},
			// 获取折扣优惠
			async getOrderDiscount() {
				const res = await getOrderDiscountApi({
					user_id: this.user_id,
					token: this.token
				})
				this.discountObj = res.data
			}
		}
	}
</script>

<style lang="scss" scoped>
@import "@/pages/aPackage/orderConfirm/orderConfirm.scss";
</style>
